import { StroageUtil } from "../../../vv/utils/StroageUtil";
import { stroageKey } from "../../constants";

//玩家实体类
export class Player {
    private static _inst: Player = null;
    public static get inst() {
        if (!this._inst) {
            this._inst = new Player();
        }
        return this._inst;
    }

    public hp: number = 0;
    public maxHp: number = 0;
    public ex: number = 0;
    public maxEx: number = 0;
    public lv: number = 0;
    public coin: number = 0; //金币
    public token: string = null;

    public init(hp: number, maxHp: number, ex: number, maxEx: number, lv: number, coin: number) {
        this.hp = hp;
        this.ex = ex;
        this.maxHp = maxHp;
        this.maxEx = maxEx;
        this.lv = lv;
        this.coin = coin;
    }

    //设置缓存
    public setCache(player?: Player) {
        if (!player) {
            player = this;
        }
        StroageUtil.setValue(stroageKey.player, player);
    }

    //获取缓存
    public getCache() {
        return StroageUtil.getObject(stroageKey.player);
    }
}
